package com.jifeng.sterilizer.service.impl;

import com.jifeng.sterilizer.utils.R;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jifeng.sterilizer.utils.PageUtils;
import com.jifeng.sterilizer.utils.Query;

import com.jifeng.sterilizer.dao.DeviceTypeDao;
import com.jifeng.sterilizer.entity.DeviceTypeEntity;
import com.jifeng.sterilizer.service.DeviceTypeService;


@Service("deviceTypeService")
public class DeviceTypeServiceImpl extends ServiceImpl<DeviceTypeDao, DeviceTypeEntity> implements DeviceTypeService {

    /**
     * @param params {"name":"消毒机","power":,"flow":,"limit":10,"cur_page":1}
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Object name = params.get("name");
        Object power = params.get("power");
        Object flow = params.get("flow");
        QueryWrapper<DeviceTypeEntity> queryWrapper = new QueryWrapper<>();
        if (!name.equals("")) {
            queryWrapper.like("name", name);
        }
        if (!power.equals("")) {
            queryWrapper.eq("power", power);
        }
        if (!flow.equals("")) {
            queryWrapper.eq("flow", flow);
        }
        IPage<DeviceTypeEntity> page = this.page(
                new Query<DeviceTypeEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public R getListByName(Map<String, Object> params) {
        List<Map<String, Object>> list = new ArrayList<>();
        Object name = params.get("name");
        List<DeviceTypeEntity> deviceTypeEntityList = this.list(new QueryWrapper<DeviceTypeEntity>().like("name", name));
        if (deviceTypeEntityList.size() > 10) {
            deviceTypeEntityList = deviceTypeEntityList.subList(0, 10);
        }
        deviceTypeEntityList.forEach(deviceTypeEntity -> {
            Map<String, Object> map = new HashMap<>();
            map.put("id", deviceTypeEntity.getId());
            map.put("name", deviceTypeEntity.getName());
            list.add(map);
        });
        return R.ok().put("data", list);
    }

    @Override
    public R del(Map<String, Object> params) {

        Object id = params.get("id");
        boolean remove = this.removeById((Serializable) id);
        if (remove) {
            return R.ok();
        }
        return R.error(400,"消毒剂类型id无效");


//        Object id = params.get("id");
//        DeviceTypeEntity deviceTypeEntity = new DeviceTypeEntity();
//        deviceTypeEntity.setEnabled(0);
//        deviceTypeEntity.setUpdateTime(new Date());
//        boolean update = this.update(deviceTypeEntity, new QueryWrapper<DeviceTypeEntity>().eq("id", id));
//        if (update) {
//            return R.ok();
//        }
//        return R.error(400,"请检查参数值是否有效");

    }

}